<?php
/**
 * AdminPortlet PHP file
 *
 * @author Steen Rabol <steen.rabol@gmail.com>
 * @link http://www.pradoportal.dk/
 * @copyright Copyright &copy; 2006,2007,2008 Steen Rabol
 * @license http://www.pradoportal.dk
 * @version $Id: AdminPortlet.php 457 2011-01-07 22:56:02Z steen.rabol $
 * @package Pradoportal.Portlets.AdminPortlet
 */

Prado::using('Application.Portlets.Portlet');
Prado::using('Application.Common.Web.UI.WebControls.TJSCookMenu.TJSCookMenu');
Prado::using('Application.Common.Data.PortalPortletRecord');
/**
 * AdminPortlet PHP class
 *
 * This is the display part of the admin menu portlet.
 *
 * @package Pradoportal.Portlets.AdminPortlet
 */

class AdminPortlet extends Portlet
{
	public function onInit($parm)
	{
		parent::onInit($parm);

		if($this->Parameters === null)
		{
			$this->Parameters = array("pagename" => $this->Service->RequestedPagePath);
		}
	}

	public function onLoad($parm)
	{
		parent::onLoad($parm);
		if($this->Application->User->isGuest || ($this->User->RoleId != $this->Application->Parameters['AdminRole']))
		{
			return;
		}
		// let only authenticated users see the menu on top
		if(!$this->Application->getModule('auth')->CanEdit($this->Parameters['pagename']))
		{
			$this->pnlAdminMenu->Visible = true;
			$this->pnlAdminMenu->Enabled = true;
		}
		else
		{
			$this->pnlAdminMenu->Visible = true;
			$this->pnlAdminMenu->Enabled = true;
		}

		// if the user is no admin remove the admin specific menus
		//TODO
		if($this->User->RoleId != $this->Application->Parameters['AdminRole'])
		{
			//$this->PageProperty->Enabled = false;
			//$this->PageProperty->Visible = false;
			//$this->CreatePage->Enabled = false;
			//$this->CreatePage->Visible = false;
		}

		$this->mnuCurrentPageEdit->Visible	= !$this->Page->EditMode;
		$this->mnuCurrentPageView->Visible	= $this->Page->EditMode;

		$canDelete = $this->canDeletePage($this->Service->RequestedPagePath);
		$this->mnuCurrentPageDelete->Visible	= $canDelete;
		$this->mnuCurrentPageDelete->Enabled	= $canDelete;

		if((strstr($this->Service->RequestedPagePath, 'System') === false) && (strstr($this->Service->RequestedPagePath, 'Admin') === false) )
		{
			$this->mnuCurrentPage->Visible			= true;
		}
		else
		{
			$this->mnuCurrentPage->Visible			= false;
		}

		// Add items to the portlet menu
		$pRecords	= PortalPortletRecord::finder()->findAllByhasadmin(1);
		if($pRecords)
		{
			foreach($pRecords as $pRecord)
			{
				if($pRecord->hasadmin)
				{
					Prado::using('Application.Portlets.' . $pRecord->name . '.Admin.' . $pRecord->name);
					$classObj	= new $pRecord->name;

					$menuitems = call_user_func(array($classObj,'getAdminMenus'));
					if($menuitems)
					{
						if(count($menuitems) == 1)
						{
							// Create an item
							$mi = $menuitems[0];
							PortalUtil::Info($mi);
							
							$pitem = new TJSCookMenuItem;
							$pitem->Title	= $mi['title'];
							$pitem->ID		= "top_" . $pRecord->name;
							if($mi['url'])
								$pitem->Url		= $this->Service->constructUrl('Admin.Portlets.' . $pRecord->name . '.' . $mi['url'],null,false);
							$this->mnuPortlets->Controls[]	= $pitem;
							
						}
						else if(count($menuitems) > 1)
						{

							$mi = $menuitems[0];
							// Create an item
							$pitem = new TJSCookMenuItem;
							$pitem->Title	= $mi['title'];
							$pitem->ID		= "top_" . $pRecord->name;
							$pitem->Url		= $this->Service->constructUrl('Admin.Portlets.' . $pRecord->name,null,false);
							//Create subitems
							unset($menuitems[0]);
							foreach ($menuitems as $mi)
							{
								$pii = new TJSCookMenuItem;
								$pii->Title		= $mi['title'];
								$pii->ID		= "top_" . $pRecord->name . uniqid();
								$pii->Url		= $this->Service->constructUrl('Admin.Portlets.' . $pRecord->name . '.' . $mi['url'],null,false);
								$pitem->Controls[] = $pii;

							}
							$this->mnuPortlets->Controls[]	= $pitem;


						}
					}
				}
			}
		}
		else
		{
			$this->mnuPortlets->Visible	= false;
		}

		// Add items to the Tools menu
		$tools = PortalUtil::getFilesInPath($this->Application->BasePath . '/Pages/Admin/Tools');
		if(is_array($tools) && count($tools) != 0)
		{
			foreach($tools as $tool)
			{
				if(strstr($tool,'.php') !== false)
				{
					$toolClassname = str_replace('.php', '', $tool);
					try
					{
						Prado::using('Application.Pages.Admin.Tools.' . $toolClassname, true);
						$menuname = call_user_func(array($toolClassname,'ToolMenuName'));
						// Create an item
						$pitem = new TJSCookMenuItem;
						$pitem->Title	= $menuname;
						$pitem->ID		= "tool_" . $toolClassname;
						$pitem->Url		= $this->Service->constructUrl('Admin.Tools.' . $toolClassname,null,false);
						$this->mnuTools->Controls[]	= $pitem;
					}
					catch(Exception $e)
					{
						// Maybe this tool do not have the correct menu...
						PortalUtil::Log(__METHOD__ . ": Faild to add  $toolClassname to the menu");
					}
				}
			}
		}
	}

	public function getHelpUrl()
	{
		$url = "";
		if(($this->Service->RequestedPagePath == 'Help') || ((substr($this->Service->RequestedPagePath,0,6) != 'System') && (substr($this->Service->RequestedPagePath,0,5) != 'Admin')))
		{
			$url = $this->Service->constructUrl('System.HelpToc');
		}
		else
		{
			$url = $this->Service->constructUrl('System.Help',array("pagename" => $this->Service->RequestedPagePath));
		}

		return $url;
	}

	public function render($writer)
	{
		$oldmode = TPropertyValue::ensureInteger($this->Page->EditMode);

		$this->Page->EditMode = 0;
		parent::render($writer);
		$this->Page->EditMode = $oldmode;
	}

	private function canDeletePage($p_path)
	{
		$ret = true;
		if(strstr($this->Service->RequestedPagePath, 'System') === true)
		{
			$ret = false;
		}
		else if(strstr($this->Service->RequestedPagePath, 'Admin') === true)
		{
			$ret = false;
		}
		else if($this->Service->RequestedPagePath === 'Home')
		{
			$ret = false;
		}


		return $ret;
	}
}
?>